package chapter06;
import java.util.ArrayList;
import java.util.Iterator;

public class StudentManagement {
    public static void main(String[] args) {
        StudentManagement stm = new StudentManagement();
        stm.addStudent(new Student("张三","110","人工智能"));
        stm.addStudent(new Student("张三","110","人工智能"));
        stm.printStudent();
        stm.deleteStudent("111");
        stm.printStudent();
        stm.checkStudent("110");
        stm.checkStudent("111");
        stm.update(new Student("李四","110","大数据技术"));
        stm.printStudent();
    }
    private ArrayList<Student> s;
    public StudentManagement() {
        this.s = new ArrayList<>();
    }
    //对学生进行操作
    //增加学生
    public void addStudent(Student e){
        boolean contains = s.contains(e);
        if(contains){
            System.out.println("重复加入");
        }else{
            s.add(e);
            System.out.println("增加学生成功");
        }
    }
    //删除学生
    public void deleteStudent(String id){
        Iterator<Student> iterator = s.iterator();
        while(iterator.hasNext()){
            Student next = iterator.next();
            if(next.getId().equals(id)){
                iterator.remove();
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("删除失败");
    }
    public boolean checkStudent(String id){
        //使用foreach方式
        for(Student e:s){
            if(e.getId().equals(id)){
                System.out.println(e);
                return true;
            }
        }
        System.out.println("未找到该学生");
        return false;
    }
    //修改学生信息
    public void update(Student e){
        boolean b = this.checkStudent(e.getId());
        if(b){
            this.deleteStudent(e.getId());
            this.addStudent(e);
        }
    }
    public void printStudent(){
        System.out.println(s);
    }
}
